﻿using System;
using System.Windows.Data;
using System.Text;
namespace ReflectionStudio.Controls
{
    /// <summary>
    /// Converter : Boolean to System.Windows.Visibility and not revert
    /// </summary>
    [ValueConversion(typeof(string), typeof(string))]
    public class GB2312Convert8859P1 : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            if (parameter != null)
            {
                string P = parameter.ToString();
             }
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(value.ToString()));
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] b;
            string filevalue = "";
            if (value == null)
                return "";
            b = Encoding.GetEncoding("gb2312").GetBytes(value as string);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue;
        }
    }

    /// <summary>
    /// Converter : Boolean to System.Windows.Visibility and not revert
    /// </summary>
    [ValueConversion(typeof(string), typeof(string))]
    public class UPPERGB2312 : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string text = value.ToString();
            Int32 I = text.Length;
            string abc = "abcdefghijklmnopqrstuvwxyz",str = "",A= "";
            
            if (I > 0)
            {               
                for (Int32 T=1; T <= I; T++)
                {
                    A = text.Substring(T - 1, 1).ToString();
                    if(abc.IndexOf(A)>-1)
                    {
                        A = A.ToUpper();
                    }
                    str = str + A;
                }
            }
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(str));
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] b;
            string filevalue = "";
            if (value == null)
                return "";
            string text = value.ToString();
            Int32 I = text.Length;
            string abc = "abcdefghijklmnopqrstuvwxyz", str = "", A = "";

            if (I > 0)
            {
                for (Int32 T = 1; T <= I; T++)
                {
                    A = text.Substring(T - 1, 1).ToString();
                    if (abc.IndexOf(A) > -1)
                    {
                        A = A.ToUpper();
                    }
                    str = str + A;
                }
            }
            b = Encoding.GetEncoding("gb2312").GetBytes(str);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue;
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class TRIMUPPERGB2312 : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            string text = value.ToString();
            Int32 I = text.Length;
            string abc = "abcdefghijklmnopqrstuvwxyz", str = "", A = "";

            if (I > 0)
            {
                for (Int32 T = 1; T <= I; T++)
                {
                    A = text.Substring(T - 1, 1).ToString();
                    if (abc.IndexOf(A) > -1)
                    {
                        A = A.ToUpper();
                    }
                    str = str + A;
                }
            }
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(str.Trim()));
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] b;
            string filevalue = "";
            if (value == null)
                return "";
            string text = value.ToString();
            Int32 I = text.Length;
            string abc = "abcdefghijklmnopqrstuvwxyz", str = "", A = "";

            if (I > 0)
            {
                for (Int32 T = 1; T <= I; T++)
                {
                    A = text.Substring(T - 1, 1).ToString();
                    if (abc.IndexOf(A) > -1)
                    {
                        A = A.ToUpper();
                    }
                    str = str + A;
                }
            }
            b = Encoding.GetEncoding("gb2312").GetBytes(str);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue.Trim();
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class TRIMGB2312Convert8859P1 : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            if (parameter != null)
            {
                string P = parameter.ToString();
            }
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(value.ToString().Trim()));
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] b;
            string filevalue = "";
            if (value == null)
                return "";
            b = Encoding.GetEncoding("gb2312").GetBytes(value as string);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue.Trim();
        }
    }
}
